package com.woniuxy.yoga.business.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.business.model.dto.businessstoreapply.StoreApplyAuditDTO;
import com.woniuxy.yoga.business.model.dto.businessstoreapply.StoreApplyDTO;
import com.woniuxy.yoga.business.model.form.businessstoreapply.AdminStoreApplyAuditForm;
import com.woniuxy.yoga.business.model.form.businessstoreapply.SelectPageAuditedForm;
import com.woniuxy.yoga.business.model.form.businessstoreapply.SelectPageNotAuditedForm;
import com.woniuxy.yoga.business.model.form.businessstoreapply.StoreApplyAddForm;
import com.woniuxy.yoga.business.model.param.businessstoreapply.AdminStoreApplyAuditParam;
import com.woniuxy.yoga.business.model.param.businessstoreapply.SelectPageAuditedParam;
import com.woniuxy.yoga.business.model.param.businessstoreapply.SelectPageNotAuditedParam;
import com.woniuxy.yoga.business.model.param.businessstoreapply.StoreApplyAddParam;
import com.woniuxy.yoga.business.permission.Permission;
import com.woniuxy.yoga.business.service.BusinessStoreApplyService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.oss.util.OSSUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 商家门店申请表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/businessStoreApply")
@Api(tags = "门店申请相关接口")
public class BusinessStoreApplyController {

    @Autowired
    private BusinessStoreApplyService businessStoreApplyService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;

    /**
     * 商家申请入驻
     * @return
     * @throws Exception
     */
    @PostMapping("/addStoreApply.bu")
    @ApiOperation("商家申请入驻")
    @YogaLog("商家申请入驻")
    public Result<StoreApplyDTO> addStoreApply(@Valid @RequestBody StoreApplyAddForm form, @RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Integer businessId = JWTUtil.getBusinessId(refreshToken);
        StoreApplyAddParam param = BeanUtil.toBean(form, StoreApplyAddParam.class);
        param.setAccountId(businessId);
        StoreApplyDTO dto = businessStoreApplyService.addStoreApply(param);
        return Result.getResult(StoreApplyDTO.class).setMessage("门店申请成功").setData(dto);
    }

    /**
     * 上传商家头像（也可以用来测试）
     * @param image
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadImage.bu")
    @ApiOperation("上传图片")
    @YogaLog("上传图片")
    public Result<String> uploadImage(MultipartFile image) throws Exception {
        if (image==null) throw new Exception("传入图片不能为空");
        //获取上传图片的文件名
        String filename = image.getOriginalFilename();
        //获取图片的类型
        String name = filename.substring(filename.lastIndexOf(".")+1);
        //调用API上传
        String imagePath = ossUtil.uploadFile(image.getInputStream(),name);
        return Result.getResult(String.class).setData(imagePath).setMessage("上传图片成功");
    }

    /**
     * 平台审核入驻申请
     * @param form
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("/auditStoreApply.api")
    @ApiOperation("平台审核入驻申请")
    @YogaLog("平台审核入驻申请")
    @Permission(resources = {"/business/businessStoreApply/auditStoreApply.api"})
    public Result<StoreApplyAuditDTO> auditStoreApply(@Valid@RequestBody AdminStoreApplyAuditForm form, @RequestHeader String authen_token)throws Exception{
        String refreshToken = redisUtil.get(authen_token);
        Claims claims = JWTUtil.parseAdminToken(refreshToken);
        Integer accountId = Integer.parseInt(claims.get("accountId").toString());
        String accountName = claims.get("accountName").toString();
        AdminStoreApplyAuditParam param = BeanUtil.toBean(form, AdminStoreApplyAuditParam.class);
        param.setAccountId(accountId);
        param.setCheckUser(accountName);
        StoreApplyAuditDTO dto = businessStoreApplyService.auditStoreApply(param);
        return Result.getResult(StoreApplyAuditDTO.class).setMessage("审核成功").setData(dto);
    }

    /**
     * 平台查询未审核门店入驻信息
     * @param form
     * @return
     */
    @PostMapping("/selPageNotAudited.api")
    @ApiOperation("平台分页查询未审核门店信息")
    @YogaLog("平台查询未审核入驻信息")
    @Permission(resources = {"/business/businessStoreApply/selPageNotAudited.api"})
    public Result<?> selPageNotAudited(@Valid @RequestBody SelectPageNotAuditedForm form){
        PageInfo pageInfo = businessStoreApplyService.selPageNotAudited(BeanUtil.toBean(form, SelectPageNotAuditedParam.class));
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("分页查询未审核信息成功");
    }

    /**
     * 平台查询已审核的入驻信息
     * @param form
     * @return
     */
    @PostMapping("/selPageAudited.api")
    @ApiOperation("平台查询已审核的入驻信息")
    @YogaLog("平台查询已审核的入驻信息")
    @Permission(resources = {"/business/businessStoreApply/selPageAudited.api"})
    public Result<PageInfo> selPageAudited(@Valid @RequestBody SelectPageAuditedForm form){
        PageInfo pageInfo = businessStoreApplyService.selPageAudited(BeanUtil.toBean(form, SelectPageAuditedParam.class));
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("分页查询已审核信息成功");
    }

}

